package com.zhengx.fragmentutil;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;

/**
 * name：com.zhengx.fragmentutil
 * class: 处理fragment生命周期相关的功能
 * author: zhengx
 * create_time: 19-9-20
 * * 用{@link androidx.fragment.app.FragmentTransaction#setMaxLifecycle(Fragment, Lifecycle.State)}
 *  * 设置生命周期，在onResume和onPause中获取是否可见就行了
 */
@Deprecated
public class FragmentHandlerDelegate {

    private FragmentVisibleHandler visibleDelegate;

    protected FragmentHandlerDelegate(Fragment fragment) {
        this.visibleDelegate = new FragmentVisibleHandler(fragment);
    }

    public void onHiddenChanged(boolean hidden) {
        visibleDelegate.onHiddenChanged(hidden);
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        visibleDelegate.setUserVisibleHint(isVisibleToUser);
    }

    public void onResume() {
        visibleDelegate.onResume();
    }

    public void onPause() {
        visibleDelegate.onPause();
    }


    /**
     * fragment可见状态判断
     */
    protected static final class FragmentVisibleHandler {

        Fragment fragment;

        /**
         * fragment是否是在viewpager中
         */
        private boolean useInViewpager;

        /**
         * 当前fragment的显示状态
         */
        private boolean isFragmentVisible;

        /**
         * fragment的容器是否可见
         */
        private boolean isHostResume;

        /**
         * 是否调用了hidden使fragment隐藏
         */
        private boolean isHidden;

        /**
         * 在viewpager中，是否显示
         */
        private boolean isVisibleToUserInFragPager;

        public FragmentVisibleHandler(Fragment fragment) {
            this.fragment = fragment;
        }

        public void onHiddenChanged(boolean hidden) {

            isHidden = hidden;
            checkFragmentStatus();
        }

        public void setUserVisibleHint(boolean isVisibleToUser) {
            if (!useInViewpager) useInViewpager = true;
            isVisibleToUserInFragPager = isVisibleToUser;

            checkFragmentStatus();
        }

        public void onResume() {
            isHostResume = true;
            checkFragmentStatus();
        }

        public void onPause() {
            isHostResume = false;
            checkFragmentStatus();
        }

        private void checkFragmentStatus() {
            if (fragment instanceof IFragmentVisibleHandler) {
                //activity 不在显示状态的情况
                if (!isHostResume && isFragmentVisible) {
                    isFragmentVisible = false;
                    ((IFragmentVisibleHandler) fragment).onFragmentInvisible();
                }
                //activity onResume,并且fragment不是在viewpager中
                if (isHostResume && !useInViewpager) {

                    if (isHidden) {
                        //不显示
                        if (isFragmentVisible) {
                            isFragmentVisible = false;
                            ((IFragmentVisibleHandler) fragment).onFragmentInvisible();
                        }
                    } else {
                        //显示
                        if (!isFragmentVisible) {
                            isFragmentVisible = true;
                            ((IFragmentVisibleHandler) fragment).onFragmentVisible();
                        }
                    }
                }
                //activity onResume,并且fragment在viewpager中
                if (isHostResume && useInViewpager) {
                    if (!isVisibleToUserInFragPager) {
                        //不显示
                        if (isFragmentVisible) {
                            isFragmentVisible = false;
                            ((IFragmentVisibleHandler) fragment).onFragmentInvisible();
                        }
                    } else {
                        //显示
                        if (!isFragmentVisible) {
                            isFragmentVisible = true;
                            ((IFragmentVisibleHandler) fragment).onFragmentVisible();
                        }
                    }

                }
            }

        }
    }
}
